package com.woniuxy.carlife.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.Param.StoreParam;
import com.woniuxy.carlife.Param.StoreUserParam;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.RandomCode;
import com.woniuxy.carlife.domain.Store;
import com.woniuxy.carlife.domain.StoreUser;
import com.woniuxy.carlife.dto.StoreinfoDto;
import com.woniuxy.carlife.mapper.StoreMapper;
import com.woniuxy.carlife.mapper.StoreUserMapper;
import com.woniuxy.carlife.service.StoreService;
import com.woniuxy.carlife.service.StoreUserService;
import com.woniuxy.carlife.util.MinioTemplate;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: StoreUserController
 *@Description: 门店账号前端控制器
 * @author: yanzhongliang     
 * @date: 2021-04-05
 */

@RestController
@Slf4j
@Api(tags = "门店账号接口信息")//用于描述接口类的相关信息，作用于类上
@RequestMapping("/store-user")
public class StoreUserController {

    //    引入门店账号数据层
    @Resource
    private StoreUserService storeUserService;
    //    引入门店账号数据层
    @Resource
    private StoreUserMapper storeUserMapper;
    @Resource
    private StoreMapper storeMapper;
    //    引入门店数据层
    @Resource
    private StoreService storeService;
    @Resource
    private StoreParam storeParam;
    //    引入文件上传配置
    @Resource
    private MinioTemplate mt;
    /**
     * 功能描述〈根据店铺账号密码登陆〉
     *
     * @throws
     * @author: yanzhongliang
     * @param: storeUserParam
     * @return: massage
     * @Date 2021/4/7 9:22
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "根据店铺账号密码登陆",notes = "<span style='color:red;'>用来根据店铺账号密码登陆的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40007,message = "登录成功",response = StoreUserParam.class),
            @ApiResponse(code=20002,message = "账号或密码错误"),
            @ApiResponse(code=40000,message = "账号或密码为空")
    })

    @GetMapping("/StoreUserNameAndPassword")
    public Result findListStoreUser(@ModelAttribute @ApiParam StoreUserParam storeUserParam) {
        log.info("findListStoreUser开始：参数：{}",storeUserParam);
        //判断账号和密码是否为空
        if (storeUserParam.getStoreUserAccount()!=null&&storeUserParam.getStoreUserPassword()!=null){
            //将前端传入账号密码传入数据层，获取返回值，返回为0 登录成功，1 登录失败
            Integer codess=  storeUserService.getStoreUser(storeUserParam);
            if(codess==0){
                log.info("getStoreUser结束：参数：{}",codess);
                return new Result(true, StatusCode.LOGIN_SUCCESSFUL, "登录成功");
            }else {
                return new Result(false, StatusCode.LOGINERROR, "账号或密码有误，请重新输入");
            }
        }else {
            return new Result(false, StatusCode.CAN_NOT_BE_EMPTY, "账号或密码为空");
        }
    }

    /**
     * 功能描述〈通过传入门店营业执照，店铺名，店主名，门店服务标签集合（[tagsName]），门店id，门店手机号，精度，纬度，门店地址，门店账号，密码〉
     * @author: yanzhongliang
     * @param:  [StoreParam]
     * @return: massage
     * @Date 2021/4/7 11:15
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "门店注册",notes = "<span style='color:red;'>用来门店注册的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40009,message = "注册成功",response = StoreParam.class),
            @ApiResponse(code=40010,message = "注册失败"),
            @ApiResponse(code=70020,message = "门店图片为空"),
            @ApiResponse(code=70022,message = "门店名为空"),
            @ApiResponse(code=70023,message = "门店账号为空"),
            @ApiResponse(code=70024,message = "门店营业执照为空"),
            @ApiResponse(code=70026,message = "门店精度为空"),
            @ApiResponse(code=70027,message = "门店纬度为空"),
            @ApiResponse(code=70028,message = "门店手机号为空"),
            @ApiResponse(code=70029,message = "门店密码为空"),
            @ApiResponse(code=70030,message = "门店名为空"),
            @ApiResponse(code=70031,message = "店长名为空"),
            @ApiResponse(code=70032,message = "门店地址为空"),
            @ApiResponse(code=70033,message = "门店服务标签为空")

    })

    @PostMapping("/FindStoreAll")
    public Result setStoreRegister(@ModelAttribute @ApiParam StoreParam storeParam) {
        log.info("setStoreRegister开始：参数：{}",storeParam);
        //判断门店图片是否为空
        if(storeParam.getStoreInfoImage()!=null){
            //判断账号是否为空
            if (storeParam.getStoreUserAccount()!=null){
                //判断手机号是否为空
                if (storeParam.getStorePhone()!=null){
                    //判断门店营业执照是否为空
                    if (storeParam.getStoreBusiness()!=null){
                        //判断门店地址是否为空
                        if (storeParam.getStoreAddress()!=null){
                            //判断纬度是否为空
                            if (storeParam.getStoreLatitude()!=null){
                                //判断精度是否为空
                                if (storeParam.getStorePrecision()!=null){
                                    //判断门店名是否为空
                                    if (storeParam.getStoreName()!=null){
                                        //判断店长名是否为空
                                        if (storeParam.getStoreShopkeeperName()!=null){
                                            //判断门店服务标签是否为空
                                            if (storeParam.getTagsName()!=null){
                                                //判断门店密码是否为空
                                                if (storeParam.getStoreUserPassword()!=null){
                                                    //将前端传入注册信息集合传入数据层，获取返回值，返回为1 注册成功，2 注册失败
                                                    Integer code= storeUserService.setStoreUser(storeParam);
                                                    if (code==1){
                                                        log.info("setStoreUser结束：参数：{}",code);
                                                        return new Result(true, StatusCode.REGISTRATION_SUCCESS, "门店注册成功，等待平台审核");
                                                    }else {
                                                        return new Result(false, StatusCode.REGISTRATION_FAILED, "门店注册失败，账号或手机号已被注册");
                                                    }
                                                }else {
                                                    return new Result(false, StatusCode.STORE_PASSWORD_EMPTY, "门店服务标签不能为空");
                                                }
                                            }else {
                                                return new Result(false, StatusCode.STORE_TAGS_EMPTY, "门店服务标签不能为空");
                                            }
                                        }else {
                                            return new Result(false, StatusCode.STORE_SHOPKEEPER_EMPTY, "店长名不能为空");
                                        }
                                    }else {
                                        return new Result(false, StatusCode.STORE_NAME_EMPTY, "门店名不能为空");
                                    }
                                }else {
                                    return new Result(false, StatusCode.PRECISION_EMPTY, "门店精度不能为空");
                                }
                            }else {
                                return new Result(false, StatusCode.LATITUDE_EMPTY, "门店纬度不能为空");
                            }
                        }else {
                            return new Result(false, StatusCode.STORE_ADDRESS_EMPTY, "门店地址不能为空");
                        }
                    }else {
                        return new Result(false, StatusCode.BUSSINESS_EMPTY, "门店营业执照不能为空");
                    }
                }else {
                    return new Result(false, StatusCode.STORE_PHONE_EMPTY, "门店手机号不能为空");
                }
            }else {
                return new Result(false, StatusCode.ACCOUNT_EMPTY, "门店账号不能为空");
            }
        }else {
            return new Result(false, StatusCode.STORE_IMAGE_EMPTY, "门店图片不能为空");
        }

    }

    /**
     * 功能描述〈营业执照上传〉
     * @author: yanzhongliang
     * @param: MultipartFile
     * @return: storeParam.getStoreBusiness()
     * @Date 2021/4/12 21:51
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "营业执照上传",notes = "<span style='color:red;'>用来上传营业执照的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 70034,message = "图片上传成功"),
            @ApiResponse(code=70024,message = "门店营业执照为空")
    })

    @PostMapping("/businessfile")
    public Result setStoreBusiness(@ModelAttribute MultipartFile storeBusiness) {
        log.info("storeBusiness上传营业执照开始: 参数: {}",storeBusiness);
        //判断传入门店营业执照是否为空
        if(storeBusiness!=null){
            try {
                LocalDate ld = LocalDate.now();
                //    设置上传文件名
                StringBuffer fileName = new StringBuffer("/");
                fileName.append(ld.getYear()).append("/")
                        .append(ld.getMonthValue()).append("/")
                        .append(ld.getDayOfMonth()).append("/")
                        .append(System.currentTimeMillis());
                String StoreBusiness = storeBusiness.getOriginalFilename().substring(storeBusiness.getOriginalFilename().lastIndexOf("."));
                fileName.append(StoreBusiness);
                //        前端上传门店图片后转化的url
                String StoreBusinessimage = mt.uploadFile(fileName.toString(), "store",
                        storeBusiness.getInputStream(),
                        storeBusiness.getSize(),
                        storeBusiness.getContentType());

                log.info("得到营业执照上传后返回的地址：参数：{}",StoreBusinessimage);
                //返回门店营业执照路径
                return new Result(true, StatusCode.IMAGE_SUCCESS, "上传门店营业执照成功",StoreBusinessimage);

            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("上传失败");

            }
        }else {
            return new Result(false, StatusCode.BUSSINESS_EMPTY, "上传门店营业执照为空，请重新上传");
        }

    }

    /**
     * 功能描述〈门店图片上传〉
     * @author: yanzhongliang
     * @param: List<MultipartFile>
     * @return: listImage
     * @Date 2021/4/12 21:51
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "门店图片上传",notes = "<span style='color:red;'>用来上传门店图片的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 70034,message = "上传成功"),
            @ApiResponse(code=70020,message = "上传门店图片为空")
    })

    @PostMapping("/film")
    public Result setStoreImage(@ModelAttribute MultipartFile storeInfoImage) {

        log.info("传入门店图片：参数：{}",storeInfoImage);
        //判断门店图片是否为空
        if (storeInfoImage!=null){
            try {
                LocalDate ld = LocalDate.now();
                //    设置上传文件名
                StringBuffer fileName = new StringBuffer("/");
                fileName.append(ld.getYear()).append("/")
                        .append(ld.getMonthValue()).append("/")
                        .append(ld.getDayOfMonth()).append("/")
                        .append(System.currentTimeMillis());
                String StoreInfoImage = storeInfoImage.getOriginalFilename().substring(storeInfoImage.getOriginalFilename().lastIndexOf("."));
                fileName.append(StoreInfoImage);
                //        前端上传门店图片后转化的url
                String storeImage = mt.uploadFile(fileName.toString(), "store",
                        storeInfoImage.getInputStream(),
                        storeInfoImage.getSize(),
                        storeInfoImage.getContentType());

                log.info("得到门店的图片上传后返回的地址：参数：{}",storeImage);
                //将上传云服务器后返回的地址返回前端
                return new Result(true, StatusCode.IMAGE_SUCCESS, "上传门店图片成功",storeImage);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("上传失败");

            }
        }else {
            return new Result(false, StatusCode.STORE_IMAGE_EMPTY, "上传门店图片为空，请重新上传");
        }

    }


}

